package com.nervous.jiaoli.user.home.impl;

import com.nervous.jiaoli.common.CurrentUser;
import com.nervous.jiaoli.user.home.HomeService;
import com.nervous.jiaoli.user.home.MenuVO;
import com.nervous.jiaoli.user.home.dao.HomeDao;
import com.nervous.jiaoli.user.home.Module;
import com.nervous.jiaoli.user.user.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional
public class HomeServiceImpl implements HomeService {
    @Resource
    private HomeDao homeDao;
    @Override
    public List<MenuVO> getMenus(CurrentUser currentUser) {
        List<Module> list = homeDao.findModuleListByUser(currentUser.getUserId());

        List<MenuVO> menuList = new ArrayList<>();
        MenuVO menu = null;//当前父菜单
        for(Module module : list){
            if(menu == null || !menu.getId().equals(module.getP_id())){
                menu = new MenuVO();
                menu.setId(module.getP_id());
                menu.setName(module.getP_name());
                menu.setUrl(module.getP_url());
                menu.setChildren(new ArrayList<MenuVO>());
                menuList.add(menu);
            }

            if (module.getSm_visible_mode() != null && module.getSm_visible_mode().intValue() == 1) {
                MenuVO child = new MenuVO();
                child.setId(module.getSm_id());
                child.setName(module.getSm_name());
                child.setUrl(module.getSm_web_route());
                menu.getChildren().add(child);
            }

        }

        return menuList;
    }
    @Override
    public User getCurrentUserAllInfo(String userId) {
        User currentAll = homeDao.findCurrentUserAllInfo(userId);
        return currentAll;
    }
}